package com.xiaoyi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.xiaoyi.bean.PanBean;
import com.xiaoyi.httplibrary.OKHttpUtils;
import com.xiaoyi.utils.FileUtilsCompat;
import com.xiaoyi.utils.OKHttpPan;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainPan {
    public static void main(String[] args) {
        getCache();




    }

    public static void getCache() {
        String str = FileUtilsCompat.getStrFromFile(FileUtilsCompat.ROOT_PATH + File.separator + "cache.ini");

        if (str.equals("")) {
            start(0,0,0,0,0,0,0,0,0);
        } else {
            try {
                JSONObject jsonObject = JSON.parseObject(str);
                int a  = jsonObject.getInteger("a");
                int b  = jsonObject.getInteger("b");
                int c  = jsonObject.getInteger("c");
                int d  = jsonObject.getInteger("d");
                int e  = jsonObject.getInteger("e");
                int f  = jsonObject.getInteger("f");
                int g  = jsonObject.getInteger("g");
                int h  = jsonObject.getInteger("h");
                int i  = jsonObject.getInteger("i");
                start(a,b,c,d,e,f,g,h,i);
            }catch (JSONException e){
                start(0,0,0,0,0,0,0,0,0);
            }

        }

    }

    public static void start(int a,int b,int c,int d,int e,int f,int g,int h,int i) {


        String[] base = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
        for (int aa = a;aa<62;aa++){
            for (int bb = b;bb<62;bb++){
                for (int cc = c;cc<62;cc++){
                    for (int dd = d;dd<62;dd++){
                        for (int ee = e;ee<62;ee++){
                            for (int ff = f;ff<62;ff++){
                                for (int gg = g;gg<62;gg++){
                                    for (int hh = h;hh<62;hh++){
                                        for (int ii = i;ii<62;ii++){
                                            String shortUrl = base[aa]+base[bb]+base[cc]+base[dd]+base[ff]+base[gg]+base[hh]+base[ii];
                                            JSONObject jsonObject = new JSONObject();
                                            jsonObject.put("a",aa);
                                            jsonObject.put("b",bb);
                                            jsonObject.put("c",cc);
                                            jsonObject.put("d",dd);
                                            jsonObject.put("e",ee);
                                            jsonObject.put("f",ff);
                                            jsonObject.put("g",gg);
                                            jsonObject.put("h",hh);
                                            jsonObject.put("i",ii);
                                            FileUtilsCompat.wirteStr2File(jsonObject.toJSONString(),FileUtilsCompat.ROOT_PATH + File.separator + "cache.ini");
                                            HashMap<String, String> map = new HashMap<>();
                                            OKHttpPan okHttpPan = new OKHttpPan();
                                            String requestUrl = "https://pan.baidu.com/s/"+shortUrl;
                                            System.out.println("-----请求----->"+requestUrl);
                                            String htmlRes = okHttpPan.doGet(requestUrl, map);
                                            String fileinfo = "yunData\\.FILEINFO = ([\\s\\S]*?);";
                                            // 编译正则表达式
                                            Pattern patternfileinfo = Pattern.compile(fileinfo);
                                            Matcher matcherfileinfo = patternfileinfo.matcher(htmlRes);
                                            if(matcherfileinfo.find()){

                                                /*
                                                yunData.SHAREPAGETYPE = "multi_file";
                                                 yunData.MYUK = "";
                                                 yunData.SHARE_USER_NAME = "gu***zkj";
                                                 yunData.SHARE_ID = "4097685468";
                                                 yunData.SIGN = "403a08adf2e8dad270628a9d6ad9b16d97ee60ec";
                                                 yunData.sign = "403a08adf2e8dad270628a9d6ad9b16d97ee60ec";
                                                 yunData.TIMESTAMP = "1547819213";
                                                 yunData.SHARE_UK = "167868409";
                                                 yunData.SHARE_PUBLIC = 1;
                                                 yunData.SHARE_TIME = "1483520204";
                                                 yunData.SHARE_DESCRIPTION = "";
                                                 yunData.MYSELF = +false;
                                                 yunData.MYAVATAR = "";

                                                 yunData.NOVELID = "";
                                                 */

/**
 * String filename = "yunData\\.FILENAME = \"([\\s\\S]*?)\";";
 * //        String path_md5 = "yunData\\.PATH_MD5 = \"([\\s\\S]*?)\";";
 * //        String ctime = "yunData\\.CTIME = \"([\\s\\S]*?)\";";
 *         String fs_id = "yunData\\.FS_ID = \"([\\s\\S]*?)\";";
 *         String share_time = "yunData\\.SHARE_TIME = \"([\\s\\S]*?)\";";
 *         String fileinfo = "yunData\\.FILEINFO = ([\\s\\S]*?);";
 */
                                                String psharepagetype = "yunData\\.SHAREPAGETYPE = \"([\\s\\S]*?)\";";
                                                Pattern patternsharepagetype = Pattern.compile(psharepagetype);
                                                Matcher matchersharepagetype = patternsharepagetype.matcher(htmlRes);
                                                String sharepagetype = "";
                                                if(matchersharepagetype.find()){
                                                    sharepagetype = matchersharepagetype.group(1);
                                                }
                                                String pmyuk = "yunData\\.MYUK = \"([\\s\\S]*?)\";";
                                                Pattern patternmyuk = Pattern.compile(pmyuk);
                                                Matcher matchermyuk = patternmyuk.matcher(htmlRes);
                                                String myuk = "";
                                                if(matchermyuk.find()){
                                                    myuk = matchermyuk.group(1);
                                                }
                                                String pshare_user_name = "yunData\\.SHARE_USER_NAME = \"([\\s\\S]*?)\";";
                                                Pattern patternSHARE_USER_NAME = Pattern.compile(pshare_user_name);
                                                Matcher matcherSHARE_USER_NAME = patternSHARE_USER_NAME.matcher(htmlRes);
                                                String share_user_name = "";
                                                if(matcherSHARE_USER_NAME.find()){
                                                    share_user_name = matchermyuk.group(1);
                                                }

                                                String pshare_id = "yunData\\.SHARE_ID = \"([\\s\\S]*?)\";";
                                                Pattern patternSHARE_ID = Pattern.compile(pshare_id);
                                                Matcher matcherSHARE_ID = patternSHARE_ID.matcher(htmlRes);
                                                String share_id = "";
                                                if(matcherSHARE_ID.find()){
                                                    share_id = matcherSHARE_ID.group(1);
                                                }

                                                String psign = "yunData\\.SIGN = \"([\\s\\S]*?)\";";
                                                Pattern patternSIGN = Pattern.compile(psign);
                                                Matcher matcherSIGN = patternSIGN.matcher(htmlRes);
                                                String sign = "";
                                                if(matcherSIGN.find()){
                                                    sign = matcherSIGN.group(1);
                                                }


                                                String ptimestamp = "yunData\\.TIMESTAMP = \"([\\s\\S]*?)\";";
                                                Pattern patternTIMESTAMP = Pattern.compile(ptimestamp);
                                                Matcher matcherTIMESTAMP = patternTIMESTAMP.matcher(htmlRes);
                                                String timestamp = "";
                                                if(matcherTIMESTAMP.find()){
                                                    timestamp = matcherTIMESTAMP.group(1);
                                                }

                                                //SHARE_UK
                                                String pshare_uk = "yunData\\.SHARE_UK = \"([\\s\\S]*?)\";";
                                                Pattern patternSHARE_UK = Pattern.compile(pshare_uk);
                                                Matcher matcherSHARE_UK = patternSHARE_UK.matcher(htmlRes);
                                                String share_uk = "";
                                                if(matcherSHARE_UK.find()){
                                                    share_uk = matcherSHARE_UK.group(1);
                                                }

                                                //SHARE_TIME
                                                String pshare_time = "yunData\\.SHARE_TIME = \"([\\s\\S]*?)\";";
                                                Pattern patternSHARE_TIME = Pattern.compile(pshare_time);
                                                Matcher matcherSHARE_TIME = patternSHARE_TIME.matcher(htmlRes);
                                                String share_time = "";
                                                if(matcherSHARE_TIME.find()){
                                                    share_time = matcherSHARE_TIME.group(1);
                                                }


                                                String group = matcherfileinfo.group(1);
                                                List<PanBean> panBeans = JSON.parseArray(group, PanBean.class);
                                                String str2File = "";
                                                for (PanBean panBean:panBeans){
                                                    System.out.println("--------找到文件------->"+panBean.getServer_filename());
                                                    panBean.setSharepagetype(sharepagetype);
                                                    panBean.setMyuk(myuk);
                                                    panBean.setShare_user_name(share_user_name);
                                                    panBean.setShare_id(share_id);
                                                    panBean.setSign(sign);
                                                    panBean.setTimestamp(timestamp);
                                                    panBean.setShare_uk(share_uk);
                                                    panBean.setShare_time(share_time);
                                                    panBean.setAddTime(""+System.currentTimeMillis());
                                                    panBean.setUrl(requestUrl);
                                                    str2File +=JSON.toJSONString(panBean)+"\n";
                                                    }

                                                if(!"".equals(str2File))
                                                    FileUtilsCompat.wirteStr2File(JSON.toJSONString(str2File),FileUtilsCompat.ROOT_PATH + File.separator + "resoult.log",true);

                                                try {
                                                    Thread.sleep(1000);
                                                } catch (InterruptedException e1) {
                                                    continue;
                                                }
                                            }else{
                                                System.out.println("没找到");
                                                continue;
                                            }

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }


}
/**
 yunData.SHAREPAGETYPE = "multi_file";

 yunData.MYUK = "";
 yunData.SHARE_USER_NAME = "gu***zkj";
 yunData.SHARE_ID = "4097685468";
 yunData.SIGN = "403a08adf2e8dad270628a9d6ad9b16d97ee60ec";
 yunData.sign = "403a08adf2e8dad270628a9d6ad9b16d97ee60ec";
 yunData.TIMESTAMP = "1547819213";
 http://pan.baidu.com/wap/share/home?uk=167868409
 yunData.SHARE_UK = "167868409";
 yunData.SHARE_PUBLIC = 1;
 yunData.SHARE_TIME = "1483520204";
 yunData.SHARE_DESCRIPTION = "";
 yunData.MYSELF = +false;
 yunData.MYAVATAR = "";

 yunData.NOVELID = "";
 yunData.FS_ID = "1082059461857471";
 yunData.FILENAME = "apk操作步骤";
 yunData.PATH = "\/sharelink167868409-1082059461857471\/apk操作步骤";
 yunData.PATH_MD5 = "12075486162494640591";
 yunData.CTIME = "1483520204";
 yunData.CATEGORY = "6";
 yunData.DOCPREVIEW = "";
 yunData.IS_BAIDUSPIDER = "";
 yunData.ARTISTNAME = "";
 yunData.ALBUMTITLE = "";
 yunData.TRACKTITLE = "";
 yunData.FILEINFO = [{"fs_id":1082059461857471,"app_id":"250528","parent_path":"%2Fsharelink167868409-1082059461857471","server_filename":"apk\u64cd\u4f5c\u6b65\u9aa4","size":0,"server_mtime":1482668183,"server_ctime":1482668183,"local_mtime":1482668183,"local_ctime":1482668183,"isdir":1,"isdelete":"0","status":"0","category":6,"share":"0","path_md5":"12075486162494640591","delete_fs_id":"0","extent_int3":"0","extent_tinyint1":"0","extent_tinyint2":"0","extent_tinyint3":"0","extent_tinyint4":"0","oper_id":"0","path":"\/sharelink167868409-1082059461857471\/apk\u64cd\u4f5c\u6b65\u9aa4","root_ns":86379065,"md5":"","file_key":""}];
 //        String filename = "yunData\\.FILENAME = \"([\\s\\S]*?)\";";
 //        String path_md5 = "yunData\\.PATH_MD5 = \"([\\s\\S]*?)\";";
 //        String ctime = "yunData\\.CTIME = \"([\\s\\S]*?)\";";
 //        String fs_id = "yunData\\.FS_ID = \"([\\s\\S]*?)\";";
 //        String share_time = "yunData\\.SHARE_TIME = \"([\\s\\S]*?)\";";
 //        String fileinfo = "yunData\\.FILEINFO = ([\\s\\S]*?);";
 //         编译正则表达式
 //        Pattern pattern = Pattern.compile(fileinfo);
 // 忽略大小写的写法
 // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
 //        Matcher matcher = pattern.matcher(s);
 // 查找字符串中是否有匹配正则表达式的字符/字符串
 //        boolean rs = matcher.find();
 //        System.out.println(rs);
 //        System.out.println(matcher.group(1));
 */


